package cn.dzhang.spring.boot.remoting;

public final class AuthenticationContextHolder {
	private static final ThreadLocal<Authentication> currentAuthentication = new ThreadLocal<Authentication>();
	private static Strategy strategy = Strategy.CURRENT;
	private static Authentication authentication;

	public enum Strategy {
		CURRENT, GLOBAL
	}

	public static void setStrategy(Strategy strategy) {
		AuthenticationContextHolder.strategy = strategy;
	}

	public static Authentication currentAuthentication() {
		switch (strategy) {
		case CURRENT:
			return currentAuthentication.get();
		case GLOBAL:
		default:
			return AuthenticationContextHolder.authentication;
		}

	}

	public static void setCurrentAuthentication(Authentication authentication) {
		switch (strategy) {
		case CURRENT:
			currentAuthentication.set(authentication);
			break;
		case GLOBAL:
		default:
			AuthenticationContextHolder.authentication = authentication;
			break;
		}
	}
}
